// pages/live/live.js
const App = getApp()
let AJAX = require("../../utils/util.js").AJAX
Page({

   /**
    * 页面的初始数据 
    */
   data: {
      navH: '',
      total: 0,
      page: 1,
      listData: [],
      messageLive: '',
      messageid: '',
      focus: false,
      toid: '',
      messageContent: '',
      isAuth: false,
      imgList: [],
      type: 2,
      member_type: 0,
      is_nodata: false,
      
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.setData({
         navH: App.globalData.navHeight,

      })

      if (App.globalData.type == 1) {
         App.globalData.type = 2

      } else {
         this.setData({
            type: App.globalData.type,

         })
      }
      this.requestList()
      this.requestAuch()
      this.membe()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {
      
   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
      this.requestList()
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {
      let vm = this
      if (vm.data.total > vm.data.listData.length) {
         vm.setData({
            page: vm.data.page + 1
         })
         vm.requestList(true)
      }
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   //图片点击事件
   imgYu: function(event) {
      var src = event.currentTarget.dataset.src; //获取data-src
      var imgList = event.currentTarget.dataset.list; //获取data-list
      //图片预览
      wx.previewImage({
         current: src, // 当前显示图片的http链接
         urls: imgList // 需要预览的图片http链接列表

      })

   },
   /**
    * member-info
    * 判断是否是教练
    */
   membe() {
      AJAX('member/member-info').then(res => {
         console.log(res.data)
         this.setData({
            member_type: res.data.member_type
         })
      })
   },
   videorequestFullScreen(e){
     this.setData({
        c_video:true
     })
     
     
     
     this.aaaaa(e);
     
     

   },

   aaaaa(e)
   {
      console.log(1111)
      console.log(e.currentTarget.dataset.id);
      this.videoContext = wx.createVideoContext(e.currentTarget.dataset.id)
      this.videoContext.requestFullScreen()
   },

   requestList(str) {
      var type = App.globalData.type
      console.log(type + 'globalData')
      let vm = this
      return new Promise((resolve, reject) => {
         AJAX('broadcast/list', "GET", {
            type: type,
            page: vm.data.page
         }).then(res => {

            let _data = []
            if (str) {
               _data = vm.data.listData
            }
            if (res.data == '') {

               this.setData({
                  is_nodata: true
               })
            } else {
               res.data.forEach(function(item, index) {
                  if (item.img) {
                     item.img = item.img.split(',')
                  } else {
                     item.img = []
                  }
                  _data.push(item)
               })
               this.setData({
                  is_nodata: false
               })
            }
            console.log(_data)
            vm.setData({
               total: res.header["X-Pagination-Total-Count"],
               listData: _data
            })
            resolve()
         })
      })
   },
   liveMessage(e) {
      let _id = e.currentTarget.dataset.id
      let vm = this
      if (!vm.data.isAuth) {
         wx.navigateTo({
            url: '/pages/authorization/authorization',
         })
         return
      }
      this.setData({
         messageLive: _id == vm.data.messageLive ? '' : _id
      })
   },
   hideMessage() {
      this.setData({
         messageLive: '',
         messageid: '',
         focus: false,
         to_id: ''
      })
   },
   jump_detail(e){
      var _id = e.currentTarget.dataset.detail
      console.log(_id)
      wx.navigateTo({
         url: '/pages/live/videoDetail/videoDetail?id=' + _id,
      })
   },
   // 点赞/取消
   // zanClick(e){
   //   let vm =this
   //   let _click = e.currentTarget.dataset.click
   //   let _id = e.currentTarget.dataset.id
   //   AJAX('broadcast/click',"POST",{
   //     broadcast_id:_id
   //   }).then(res=>{
   //     wx.showToast({
   //       title: _click?"点赞成功":"取消成功",
   //     })
   //     vm.setData({
   //       messageLive:''
   //     })
   //     vm.requestList()
   //   })
   // },
   zanClick(e) {
      let vm = this
      let _click = e.currentTarget.dataset.click
      let _id = e.currentTarget.dataset.id
      AJAX('broadcast/click', "POST", {
         broadcast_id: _id
      }).then(res => {
         vm.requestList().then(() => {
            wx.showToast({
               title: !_click
            })
         })
      })
   },
   // 评论
   messageClick(e) {
      let vm = this
      let _id = e.currentTarget.dataset.id
      let _toid = e.currentTarget.dataset.toid
      if (!vm.data.isAuth) {
         wx.navigateTo({
            url: '/pages/authorization/authorization',
         })
         return
      }

      this.setData({
         messageid: _id,
         focus: true,
         toid: _toid
      })
   },
   sendMessage(e) {
      let _val = e.detail.value
      let vm = this
      AJAX('broadcast/send-msg', 'POST', {
         broadcast_id: vm.data.messageid,
         content: _val,
         to_id: vm.data.toid || ''
      }).then(res => {
         wx.showToast({
            title: "评论成功",
         })
         vm.setData({
            messageid: '',
            focus: false,
            to_id: ''
         })
         vm.requestList()
      })
   },
   navBack() {
      let vm = this
      if (!vm.data.isAuth) {
         return wx.navigateTo({
            url: '/pages/authorization/authorization',
         })
      }
      wx.navigateTo({
         url: `/pages/issue/issue?type=${this.data.type}`,
      })
   },
   requestAuch() {
      let vm = this
      AJAX("member/auth").then(res => {
         vm.setData({
            isAuth: res.data.auth
         })
      })
   },
   // 展示图片
   showImg(e) {
      let _list = e.currentTarget.dataset.list
      let _url = e.currentTarget.dataset.index
      wx.previewImage({
         urls: _list,
         current: _url
      })
   },
   // 点击切换
   headNavClick(e) {

      let _index = e.currentTarget.dataset.index
      console.log(_index + 'index')
      this.setData({
         type: _index,
         page: 1,
      })
      App.globalData.type = _index
      this.requestList(false)
      console.log(App.globalData.type + '下')
   },
   //删除
   delBroadcast(e) {
      let id = e.currentTarget.dataset.id
      AJAX('broadcast/del', 'GET', {
         id: id
      }).then(res => {
         if (res.status == "succeed") {
            wx.showToast({
               title: '删除成功',
            })
            this.requestList()
         } else {
            wx.showToast({
               icon: none,
               title: '删除失败',
            })
         }
         console.log(res);
      })
   }
})